<?php

namespace Admin\Controller;

/**
 * 权限管理之角色操作类
 * Class RoleController
 * @package Admin\Controller
 */
class RoleController extends IndexController
{
    /**
     * 角色列表
     */
    public function listRole()
    {
        $keyword = I('keyword', '', 'trim');
        $where = array();
        //角色名称查询
        if ($keyword !== '') {
            // 解决分页跳转传过的中文参数的编码问题
            $encode = mb_detect_encoding($keyword, array("ASCII", "UTF-8", "GB2312", "GBK", "BIG5"));
            if ($encode == 'EUC-CN') {
                $keyword = iconv('GBK', 'UTF-8', $keyword);
            }
            // 解决分页跳转传过的中文参数的编码问题 

            $where['role_name'] = array('like', '%' . $keyword . '%');
        }
        $roleModel = M('Role');
        $list = $this->lists($roleModel, $where, 'id desc');
        $this->assign('list', $list);
        $this->assign('keyword', $keyword);
        $this->display('list_role');
    }

    /**
     * 添加角色
     */
    public function addRole()
    {
        if (IS_POST) {
            $roleModel = D('Admin/Role');
            if ($roleModel->create(I('post.'), 1)) {
                if ($id = $roleModel->add()) {
                    $this->success('角色添加成功!');
                    exit;
                }
            }
            $this->error($roleModel->getError());
        }
        // 取出所有的权限
        $priModel = D('Admin/Privilege')->where('show_on_menubar=0');
        $priData = $priModel->getTree();
        $this->assign('priData', $priData);
        $this->display('Role:add_role');
    }

    /**
     * 编辑角色
     */
    public function editRole()
    {
        if (IS_POST) {
            $roleModel = D('Admin/Role');
            if ($roleModel->create(I('post.'), 2)) {
                if ($roleModel->save() !== FALSE) {
                    $this->success('修改成功!');
                    exit;
                }
            }
            $this->error($roleModel->getError());
        }
        // 根据id取出角色信息
        $id = I('get.id');
        $data = M('Role')->find($id);

        // 取出所有的权限
        $priModel = D('Admin/Privilege')->where('show_on_menubar=0');
        $priData = $priModel->getTree();

        // 取出当前角色所拥有的权限的ID
        $rpData = M('RolePrivilege')->field('pri_id')->where(array('pri_role_id' => array('eq', $id)))->select();
        $pri_ids = array();
        foreach ($rpData as $rp) {
            $pri_ids[] = $rp['pri_id'];
        }
        $pri_ids_str = implode(',', $pri_ids);
        $this->assign('data', $data);
        $this->assign('priData', $priData);
        $this->assign('pri_id', $pri_ids_str);
        $this->display('edit_role');
    }

    /**
     * AJAX删除角色
     */
    public function ajaxDeleteRole()
    {
        if (IS_AJAX) {
            $roleModel = D('Admin/Role');
            if ($roleModel->delete(I('get.id', 0)) !== false) {
                $this->success('角色已删除!', '', true);
            } else {
                $this->ajaxReturn(array('status' => '0', 'msg' => $roleModel->getError()));
            }
        } else {
            $this->error('非法请求');
        }
    }
}